﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace weba.Sales
{
    public partial class PurchaseOrderHeaderList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                //实例化订货单表头业务逻辑对象
                var bll =
                    mk.l.Sales_PurchaseOrderHeader.ToList();
                //设置经办人
                Accounts_Users currentUser =
                    mk.l.Accounts_Users.FirstOrDefault();
                //设置根部门ID
           var bllDept =
                    mk.l.Accounts_Department.ToList();
                this.lblRootDepartmentID.Text =
                    bllDept.Where(h=>h.ClassID==(int.Parse(currentUser.DepartmentID))).FirstOrDefault().RootID.ToString();
                this.lblStrWhere.Text =
                    " RootDepartmentID=" + this.lblRootDepartmentID.Text;
                //设置分页控件的记录总数
              
                this.BindData();   //绑定数据到GridView控件
                //为设置分页的按钮指定客户端单击事件
                
                this.BindManufacturerID();  //绑定搜索用的工厂列表
            }
        }
        private void BindManufacturerID()
        {
      var bll = mk.l.Sales_Manufacturer.ToList();
     var ds = bll.ToList();
            this.ddlManufacturerID.DataSource = ds;
            this.ddlManufacturerID.DataTextField = "ManufacturerName";
            this.ddlManufacturerID.DataValueField = "ManufacturerID";
            this.ddlManufacturerID.DataBind();
            ListItem li = new ListItem("不限", "0");
            this.ddlManufacturerID.Items.Add(li);
            this.ddlManufacturerID.Items.FindByValue("0").Selected = true;
        }

        /// <summary>
        /// 绑定订单列表
        /// </summary>
        private void BindData()
        {
         var bll = mk.l.Sales_PurchaseOrderHeader.ToList();
         var ds = bll.ToList();
            this.myGridView.DataSource = ds;
            this.myGridView.DataBind();
        }
        protected void AspNetPager_PageChanged(object sender, EventArgs e)
        {
            this.BindData();
        }
        protected void btnSetPageSize_Click(object sender, EventArgs e)
        {
            if (this.txtPageSize.Text.Trim() != "")
            {
                if (this.txtPageSize.Text.Trim() != "0")
                {
                  
                    this.BindData();
                }
                else
                {
                   
                    this.BindData();
                }
            }
            else
            {
              
                this.BindData();
            }
        }
         protected void myGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Label lblManufacturerName = (Label)(e.Row.FindControl("lblManufacturerName"));
                var bllManufacturer = mk.l.Sales_Manufacturer.ToList();
             Sales_Manufacturer mManufacturer = bllManufacturer.Where(h=>h.ManufacturerID==int.Parse(this.myGridView.DataKeys[e.Row.RowIndex].Values["ManufacturerID"].ToString())).FirstOrDefault();
                lblManufacturerName.Text = mManufacturer.ManufacturerName;

            }
        }
         protected void btnSearch_Click(object sender, EventArgs e)
         { }
         protected void myGridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
         {
             this.lblMsg.Text = string.Empty;
             Sales_PurchaseOrderHeader bll = mk.l.Sales_PurchaseOrderHeader.Single(h => h.PurchaseOrderID.ToString() == this.myGridView.DataKeys[e.RowIndex].Values["PurchaseOrderID"].ToString());
             try
             {
                 mk.l.Sales_PurchaseOrderHeader.DeleteOnSubmit(bll);
                 mk.l.SubmitChanges();
                // bll.Delete(int.Parse(this.myGridView.DataKeys[e.RowIndex].Values["PurchaseOrderID"].ToString()));
                 this.BindData();
                 //提交事务
                // ContextUtil.SetComplete();
             }
             catch (Exception ex)
             {
                 //撤销事务
               //  ContextUtil.SetAbort();
                 this.lblMsg.Text = "删除失败！错误信息：" + ex.Message;
             }
         }
    }
}